package com.vpclub.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author : leimingchao
 * @Description:
 * @date Date : 2021年06月07日 21:58
 */
@Repository
public class BookDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 1.减余额
     *
     * 减去某个用户的余额
     */
    public void updateBalance(String username,int price) {
        String sql = "updateAAA account set balance = balance - ? where username = ?";
        jdbcTemplate.update(sql,price,username);
    }

    /**
     * 2.按照图书的ISBN获取某本图书的价格
     * @return
     */
    public int getPrice(String isbn) {
        String sql = "select price from book where isbn = ?";
        Integer price = jdbcTemplate.queryForObject(sql, Integer.class, isbn);
        return price;
    }

    /**
     * 3.减库存:减去某本书的库存，为了简单起见，每次减一
     */
    public void updateStock(String isbn) {
        String sql = "update book_stock set stock = stock - 1 where isbn = ?";
        jdbcTemplate.update(sql,isbn);
    }
}
